
const { ccclass, property } = cc._decorator;

@ccclass
export default class ItemCtrl extends cc.Component {

    @property(cc.Node)
    bg: cc.Node = null;

    @property(cc.Label)
    index: cc.Label = null;

    @property(cc.Label)
    score: cc.Label = null;

    @property(cc.Label)
    nName: cc.Label = null;

    @property(cc.Sprite)
    img: cc.Sprite = null;

    setData(data, index, extData) {
        console.log(index);
        console.log(data);
        console.log(extData);
        if (data && data.KVDataList && data.KVDataList.length > 0) {
            this.index.string = index + 1 + "";
            this.score.string = data.KVDataList[0].value;
            this.nName.string = data.nickname;
            if (data.avatarUrl) this.showHead(this.img, data.avatarUrl);
            let isMy = data.openid == extData.openid;
            this.bg.active = isMy;
            this.score.node.color = isMy ? new cc.Color().fromHEX("#C05239") : new cc.Color().fromHEX("#000000");
        } else if (extData["name"]) {
            this.index.string = index + 1 + "";
            this.score.string = extData.score;
            this.nName.string = extData.name;
            if (extData.imgUrl) this.showHead(this.img, extData.imgUrl);
        }
    }

    showHead(headImg, headurl) {
        cc.assetManager.loadRemote(headurl + "?aaa=aa.jpg", (err, texture: any) => {
            if (err) {
                console.error(err);
                return;
            }
            if (texture) headImg.spriteFrame = new cc.SpriteFrame(texture);
        });
    }
}
